//
//  GestureView.m
//  UI04_触摸_手势
//
//  Created by Scott on 16/5/6.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "GestureView.h"

@implementation GestureView


#pragma mark - Creating and Init
+ (instancetype)gestureViewWithFrame:(CGRect)frame image:(UIImage *)image {
    
    GestureView *view = [[GestureView alloc] initWithImage:image];
    view.frame = frame;
    
    // 用户交互.
    view.userInteractionEnabled = YES;
    
    // 添加手势.
    [view addTap];
    [view addLongPress];
    [view addSwip];
    [view addPinch];
    [view addRotate];
    [view addPan];
    
    return [view autorelease];
}

#pragma mark - 手势
/** 轻拍手势. */
- (void)addTap {
    
    // 创建手势对象
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    
    // 手势加到视图上.
    [self addGestureRecognizer:tap];
    [tap release];
    
}
- (void)handleTap:(UITapGestureRecognizer *)tap {
    
    NSLog(@"%s", __FUNCTION__);
    
}

/** 长按手势. */
- (void)addLongPress {
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLong:)];
    
    [self addGestureRecognizer:longPress];
    [longPress release];
}

- (void)handleLong:(UILongPressGestureRecognizer *)longPress {
    
    // 在手势识别开始识别时.
    if (longPress.state == UIGestureRecognizerStateBegan ) {
        
        NSLog(@"%s", __FUNCTION__);
    }
    
}

/** 轻扫手势. */
- (void)addSwip {
    
    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwip:)];
    
    [self addGestureRecognizer:swip];
    
    [swip release];
}
- (void)handleSwip:(UISwipeGestureRecognizer *)swip {
    
    NSLog(@"%s", __FUNCTION__);
    
}

/** 捏合手势. */
- (void)addPinch {
    
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
    
    [self addGestureRecognizer:pinch];
    [pinch release];
}

- (void)handlePinch:(UIPinchGestureRecognizer *)pinch {
    
    //Core API: 设置transform属性.
    self.transform = CGAffineTransformScale(self.transform, pinch.scale, pinch.scale);
    
    pinch.scale = 1.0f;
    
}

/** 旋转手势. */
- (void)addRotate {
    
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotation:)];
    [self addGestureRecognizer:rotation];
    [rotation release];

}

- (void)handleRotation:(UIRotationGestureRecognizer *)rotation {
    
    // Core API:
    self.transform = CGAffineTransformRotate(self.transform, rotation.rotation);
    
    rotation.rotation = 0.f;

}

/** 拖拽手势. */
- (void)addPan {
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    
    [self addGestureRecognizer:pan];
    [pan release];

}


- (void)handlePan:(UIPanGestureRecognizer *)pan {
    
    
    CGPoint point = [pan translationInView:self];
    
    
    // Core API
    self.transform = CGAffineTransformTranslate(self.transform, [pan translationInView:self].x, point.y);
    
    
    [pan setTranslation:CGPointZero inView:self];
    
    
}

@end
